To create a sprite animation in an application, you first create a sprite world to contain your sprites. To do this, you perform the following steps:
The sample code function CreateSpriteStuff , shown in Listing 4-1 , calculates the bounds of the destination window and calls NewGWorld to create a new sprite layer graphics world. It then calls LockPixels to lock the pixel map of the sprite layer graphics world.
Next, CreateSpriteStuff calls NewSpriteWorld to create a new sprite world, passing the destination graphics world ( WindowPtr ) and the sprite layer graphics world. CreateSpriteStuff passes a background color to NewSpriteWorld instead of specifying a background graphics world. The newly created sprite world is returned in the global variable gSpriteWorld.
Finally, CreateSpriteStuff calls the sample code function CreateSprites to populate the sprite world with sprites.
Listing 1 Creating a sprite world
// global variables
GWorldPtr spritePlane = nil;
SpriteWorld gSpriteWorld = nil;
Rect gBounceBox;
RGBColor gBackgroundColor;
void CreateSpriteStuff (Rect *windowBounds, CGrafPtr windowPtr)
{
OSErr err;
Rect bounds;
// calculate the size of the destination
bounds = *windowBounds;
OffsetRect (&bounds, -bounds.left, -bounds.top);
gBounceBox = bounds;
InsetRect (&gBounceBox, 16, 16);
// create a sprite layer graphics world with a bit depth of 16
NewGWorld (&spritePlane, 16, &bounds, nil, nil, useTempMem);
if (spritePlane == nil)
NewGWorld (&spritePlane, 16, &bounds, nil, nil, 0);
if (spritePlane)
{
LockPixels (spritePlane->portPixMap);
gBackgroundColor.red = gBackgroundColor.green =
gBackgroundColor.blue = 0;
// create a sprite world
err = NewSpriteWorld (&gSpriteWorld, (CGrafPtr)windowPtr,
spritePlane, &gBackgroundColor, nil);
// create sprites
CreateSprites ();
}
}
| Previous | Chapter Contents | Chapter Top | Next |